package com.approval.approvalapp.center.index;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.BmcjBean;
import com.approval.approvalapp.bean.MoniterBean;
import com.approval.approvalapp.bean.SjszjBean;
import com.approval.approvalapp.bean.SqxList;
import com.approval.approvalapp.bean.SxBean;
import com.approval.approvalapp.index.SearchInputActivity;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SearchWebActivity extends BaseActivity {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    EditText titleTv1;
    @Bind(R.id.sjszj_tv)
    TextView sjszjTv;
    @Bind(R.id.sjszj_ll)
    LinearLayout sjszjLl;
    @Bind(R.id.sxq_tv)
    TextView sxqTv;
    @Bind(R.id.sxq_ll)
    LinearLayout sxqLl;
    @Bind(R.id.bmcj_tv)
    TextView bmcjTv;
    @Bind(R.id.bmcj_ll)
    LinearLayout bmcjLl;
    @Bind(R.id.sx_tv)
    TextView sxTv;
    @Bind(R.id.sx_ll)
    LinearLayout sxLl;
    @Bind(R.id.commit_tv)
    TextView commitTv;
    @Bind(R.id.cz_tv)
    TextView czTv;
    private List<MoniterBean> sjszjBeanslist = new ArrayList<>();
    private List<MoniterBean> qsxBeanslist = new ArrayList<>();
    private List<MoniterBean> bmcjBeanslist = new ArrayList<>();
    private List<MoniterBean> sxBeanslist = new ArrayList<>();


    private String areacode;
    private String id;
    private String DEPTID;
    private String key;


    @Override
    public int getLayoutId() {
        return R.layout.activity_search_web;
    }

    @Override
    public void initView() {
        searchEt();
    }


    @Override
    public void initData() {

    }


    private void searchEt() {
        titleTv1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //点击搜索的时候隐藏软键盘
                    hideKeyboard(titleTv1);
                    // 在这里写搜索的操作,一般都是网络请求数据
                    if (titleTv1.getText().toString().length() == 0) {
                        Toast.makeText(SearchWebActivity.this, "请输入搜索内容", Toast.LENGTH_SHORT).show();
                        return true;
                    }

                    Intent intent = new Intent();
                    intent.setClass(SearchWebActivity.this, SearchInputActivity.class);
                    String url = "http://58.16.65.112:84/default.aspx?Temp=1455&Areaid="
                            + SpUtil.getString(SearchWebActivity.this, SpUtil.REGION_CODE, "") + "&key="
                            + titleTv1.getText().toString();
                    intent.putExtra("url", url);
                    startActivity(intent);


                    return true;
                }

                return false;
            }
        });
    }

    public void hideKeyboard(View view) {
        InputMethodManager manager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }


    @OnClick({R.id.sjszj_ll, R.id.sxq_ll, R.id.bmcj_ll, R.id.sx_ll, R.id.commit_tv, R.id.cz_tv, R.id.back_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.sjszj_ll:
                sjszjBeanslist.clear();

                sxqTv.setText("请选择");
                bmcjTv.setText("请选择");
                sxTv.setText("请选择");
                id = "";
                DEPTID = "";
                key = "";

                NetRequsetUtil.getInstance().netRequestGet("v1/user/get_sj_info", new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        final List<SjszjBean> sjszjBeans = JSON.parseArray(str, SjszjBean.class);
                        for (int i = 0; i < sjszjBeans.size(); i++) {
                            sjszjBeanslist.add(new MoniterBean(sjszjBeans.get(i).getAreacode(),
                                    sjszjBeans.get(i).getName()));
                        }

                        OptionsPickerView pvOptions = new OptionsPickerBuilder(SearchWebActivity.this, new OnOptionsSelectListener() {
                            @Override
                            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                                areacode = sjszjBeanslist.get(options1).getId();
                                String tx = sjszjBeanslist.get(options1).getName();
                                sjszjTv.setText(tx);
                                if (tx.equals("省级")) {
                                    sxqLl.setVisibility(View.GONE);
                                } else {
                                    sxqLl.setVisibility(View.VISIBLE);
                                }
                            }
                        }).build();
                        pvOptions.setPicker(sjszjBeanslist, null, null);
                        pvOptions.show();

                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {

                    }

                    @Override
                    public void finish() {

                    }
                });
                break;
            case R.id.sxq_ll:
                qsxBeanslist.clear();


                bmcjTv.setText("请选择");
                sxTv.setText("请选择");

                DEPTID = "";
                key = "";

                if (TextUtils.isEmpty(areacode)) {
                    Toast.makeText(this, "请选择省级、市州级", Toast.LENGTH_SHORT).show();
                    return;
                }

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("areacode", areacode);
                NetRequsetUtil.getInstance().netRequestPostJson("v1/user/get_sqx_info", jsonObject.toString(), new NetDownResponse() {
                    @Override
                    public void success(String str) {
                        List<SqxList> sqxLists = JSON.parseArray(str, SqxList.class);
                        for (int i = 0; i < sqxLists.size(); i++) {
                            qsxBeanslist.add(new MoniterBean(sqxLists.get(i).getID(),
                                    sqxLists.get(i).getNAME()));
                        }

                        OptionsPickerView pvOptions = new OptionsPickerBuilder(SearchWebActivity.this, new OnOptionsSelectListener() {
                            @Override
                            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                                id = qsxBeanslist.get(options1).getId();
                                String tx = qsxBeanslist.get(options1).getName();
                                sxqTv.setText(tx);

                            }
                        }).build();
                        pvOptions.setPicker(qsxBeanslist, null, null);
                        pvOptions.show();

                    }

                    @Override
                    public void errowithresponse(String str) {

                    }

                    @Override
                    public void erro() {

                    }

                    @Override
                    public void finish() {

                    }
                });
                break;
            case R.id.bmcj_ll:
                bmcjBeanslist.clear();


                sxTv.setText("请选择");

                key = "";

                if(sxqLl.getVisibility()==View.VISIBLE){
                    if (TextUtils.isEmpty(id)) {
                        Toast.makeText(this, "请选择市 (县、区)", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }else {
                    if (TextUtils.isEmpty(areacode)) {
                        Toast.makeText(this, "省级、市州级", Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                JSONObject jsonObject1 = new JSONObject();
                if(sxqLl.getVisibility()==View.VISIBLE){
                    jsonObject1.put("ID", id);
                }else {
                    jsonObject1.put("ID", areacode);
                }
                NetRequsetUtil.getInstance().netRequestPostJson("v1/user/get_bmcj_info",
                        jsonObject1.toString(), new NetDownResponse() {
                            @Override
                            public void success(String str) {
                                List<BmcjBean> sqxLists = JSON.parseArray(str, BmcjBean.class);
                                for (int i = 0; i < sqxLists.size(); i++) {
                                    bmcjBeanslist.add(new MoniterBean(sqxLists.get(i).getDEPTID(),
                                            sqxLists.get(i).getNAME()));
                                }

                                OptionsPickerView pvOptions = new OptionsPickerBuilder(SearchWebActivity.this, new OnOptionsSelectListener() {
                                    @Override
                                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                                        DEPTID = bmcjBeanslist.get(options1).getId();
                                        String tx = bmcjBeanslist.get(options1).getName();
                                        bmcjTv.setText(tx);

                                    }
                                }).build();
                                pvOptions.setPicker(bmcjBeanslist, null, null);
                                pvOptions.show();
                            }

                            @Override
                            public void errowithresponse(String str) {

                            }

                            @Override
                            public void erro() {

                            }

                            @Override
                            public void finish() {

                            }
                        });
                break;
            case R.id.sx_ll:
                sxBeanslist.clear();
                if (TextUtils.isEmpty(DEPTID)) {
                    Toast.makeText(this, "部门、村居", Toast.LENGTH_SHORT).show();
                    return;
                }
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("DEPTID", DEPTID);
                NetRequsetUtil.getInstance().netRequestPostJson("v1/user/get_sx_info",
                        jsonObject2.toString(),
                        new NetDownResponse() {
                            @Override
                            public void success(String str) {
                                List<SxBean> sqxLists = JSON.parseArray(str, SxBean.class);
                                for (int i = 0; i < sqxLists.size(); i++) {
                                    sxBeanslist.add(new MoniterBean(sqxLists.get(i).getId(),
                                            sqxLists.get(i).getSXZXNAME()));
                                }

                                OptionsPickerView pvOptions = new OptionsPickerBuilder(SearchWebActivity.this, new OnOptionsSelectListener() {
                                    @Override
                                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                                        key = sxBeanslist.get(options1).getName();
                                        sxTv.setText(key);

                                    }
                                }).build();
                                pvOptions.setPicker(sxBeanslist, null, null);
                                pvOptions.show();
                            }

                            @Override
                            public void errowithresponse(String str) {

                            }

                            @Override
                            public void erro() {

                            }

                            @Override
                            public void finish() {

                            }
                        });
                break;
            case R.id.commit_tv:
            if (TextUtils.isEmpty(key)){
                Toast.makeText(this, "请选择事项", Toast.LENGTH_SHORT).show();
                return;
            }
                Intent intent = new Intent();
                intent.setClass(SearchWebActivity.this, SearchInputActivity.class);
                String url = "http://58.16.65.112:84/default.aspx?Temp=1455&depid="
                        + DEPTID + "&key="
                        + key;
                intent.putExtra("url", url);
                startActivity(intent);
                break;
            case R.id.cz_tv:
                sjszjTv.setText("请选择");
                sxqTv.setText("请选择");
                bmcjTv.setText("请选择");
                sxTv.setText("请选择");
                areacode = "";
                id = "";
                DEPTID = "";
                key = "";
                sxqLl.setVisibility(View.VISIBLE);
                break;
        }
    }
}
